package com.tdwyz.fictionmanage.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<E> {
    //响应码 0-success 1-defeat
    private int code;
    //提示信息
    private String message;
    //java中的泛型
    private E data;
    //成功的行为方法
    public static <E> Result<E> success(E data){
        return new Result<>(0,"操作成功",data);
    }
    //失败的行为方法
    public static <E> Result<E> defeat(String message){
        return new Result<>(1,message,null);
    }

    /**
     * 通过行数返回成功或失败的响应
     * @param updated
     * @return
     * @param <E>
     */
    public static <E> Result<E> toAjax(int updated){
        return updated==1 ? success(null) : defeat("修改失败");
    }

    /**
     * 通过新增或修改的真或假结果 返回成功或者失败响应
     * @param added
     * @return
     * @param <E>
     */
    public static <E> Result<E> toAjax(boolean added){
        return added ? success(null) : defeat("新增失败");
    }
}
